var app = new Vue({
    el: '.box',
    data: {
        nav: '',
        item: '',
        item_data: '',
        leng: '',
        open: false,
        num: 16,
        page: 0,
        refreshing: false,
        loading: false,
        text: 'List',
        loaded_all: false,
        panel: '',
        tag: '',
        new_items: '',
        hot_items: '',
        expands: true,
        links: '',
        value1: '',
        openSimple: false,
        msgs: '关键字不能为空哦~',
        ad_data: '',
        like_arr: [],
        openAlert: false
    },
    methods: {
        refresh() {
            this.refreshing = true;
            this.$refs.container.scrollTop = 0;
            setTimeout(() => {
                location.reload()
            }, 2000)
        }, load() {
            this.loading = true;
            setTimeout(() => {
                this.loading = false;
                this.page++;
                var len = this.item_data.length;
                this.leng = Math.ceil(len / this.num);
                if(this.page >= this.leng) {
                this.loaded_all = true;
                console.log(1)
            };
            for (var i = (this.page * this.num); i < (this.page * this.num) + this.num; i++) {
                if (this.item_data[i]) {
                    this.item.push(this.item_data[i])
                }
            }
        }, 1500)
		}, toggle(panel) {
    this.panel = panel === this.panel ? '' : panel
}, set_tips(obj, flag) {
    if (flag) {
        return '更新时间: ' + obj
    } else {
        return '已有 ' + obj + ' 人看过'
    }
}, openSimpleDialog() {
    this.openSimple = true
}, closeSimpleDialog() {
    this.openSimple = false
}, like_click(index, id) {
    var liles = document.querySelectorAll('.like');
    liles[index].classList.add('like_clicks');
    this.like_arr = localStorage.getItem('like') ? JSON.parse(localStorage.getItem('like')) : [];
    var len = this.like_arr.length || 0;
    for (var i = 0; i < len; i++) {
        if (id == this.like_arr[i]) {
            return false
        }
    };
    this.like_arr.push(id);
    var arr = Array.from(new Set(this.like_arr));
    localStorage.setItem('like', JSON.stringify(arr));
    get_info(109, id)
}, gift_click(index, id) {
    console.log(index, id);
    this.openAlert = true
}, openAlertDialog() {
    this.openAlert = true
}, closeAlertDialog() {
    this.openAlert = false
}
	}
});

function get_info(id) {
    axios({
        method: "POST",
        baseURL: '../api/index.php',
        data: JSON.stringify({
            head: id
        })
    }).then(function (res) {
        if (res.data.code === '200') {
            set_vue(res.data.data, id)
        } else {
            console.log(res.data.msg)
        }
    }).
        catch(function (err) {
            console.log(err)
        })
};

function set_vue(obj, id) {
    var d = obj;
    var len = d.length;
    if (id === 108) {
        for (var i = 0; i < len; i++) {
            if (d[i].ad_img) {
                if (d[i].ad_img.includes('../../../')) {
                    d[i].ad_img = d[i].ad_img.split('../../')[1]
                }
            }
        };
        app.ad_data = d
    } else {
        for (var i = 0; i < len; i++) {
            d[i].href = './item/' + d[i].id + '.html';
            if (d[i].book_img) {
                if (d[i].book_img.includes('../../../')) {
                    d[i].book_img = d[i].book_img.split('../../')[1]
                }
            } else {
                d[i].book_img = '.' + img_null
            }
        }
    };
    if (id === 100) {
        app.nav = obj
    };
    if (id === 102) {
        app.tag = obj
    };
    if (id === 103) {
        app.new_items = obj
    };
    if (id === 104) {
        app.hot_items = obj
    };
    if (id === 105) {
        app.links = obj
    };
    if (id === 106) {
        app.item = d;
        app.item_data = d;
        setTimeout(function () {
            var arr = localStorage.getItem('like') ? JSON.parse(localStorage.getItem('like')) : [];
            var len = arr.length || 0;
            var liles = document.querySelectorAll('.like');
            var liles_len = liles.length || 0;
            for (var i = 0; i < len; i++) {
                for (var j = 0; j < liles_len; j++) {
                    if (arr[i] == app.item[j].Id) {
                        liles[j].classList.add('like_clicks')
                    }
                }
            }
        }, 500)
    }
};
get_info(100);
get_info(102);
get_info(103);
get_info(104);
get_info(105);
get_info(108);

function search(obj) {
    if (obj) {
        axios({
            method: "POST",
            baseURL: '../api/index.php',
            data: JSON.stringify({
                head: 106,
                key: obj
            })
        }).then(function (res) {
            if (res.data.code === '200') {
                set_vue(res.data.data, 106)
            } else {
                app.openSimple = true;
                app.msgs = '抱歉没有找到，请尝试更换关键字~';
                console.log(res.data.msg)
            }
        }).
            catch(function (err) {
                console.log(err)
            })
    } else {
        app.msgs = '关键字不能为空哦';
        app.openSimple = true
    }
};
window.onresize = function () {
    var w = document.documentElement.clientWidth || document.body.clientWidth;
    if (w <= 830) {
        app.expands = false
    } else {
        app.expands = true
    }
};

function get_user_info() {
    var data = {
        href: window.location.href,
        title: document.title,
        timer: 5000,
        source: document.referrer
    };
    axios({
        method: "POST",
        baseURL: '../api/log.php',
        headers: {
            "Content-type": "application/json; charset=utf-8"
        },
        data: JSON.stringify(data)
    }).then(function (res) { }).
        catch(function (err) {
            console.log(err)
        })
};
setTimeout(function () {
    get_user_info()
}, 5000);